<?php
/**行为管理
 * Created by leiying
 * Date: 2018-03-09
 * Time: 15:09:36
 */

namespace Admin\Controller;



class ActionController extends AdminController
{

    /**行为日志列表
     * @author leiying
     */
    public function actionLog(){
        //获取列表数据
        $map['status']    =   array('gt', -1);
        $list   =   $this->lists('ActionLog', $map);
        int_to_string($list);
        foreach ($list as $key=>$value){
            $model_id                  =   get_document_field($value['model'],"name","id");
            $list[$key]['model_id']    =   $model_id ? $model_id : 0;
        }
        $this->assign('_list', $list);
        $this->meta_title = '行为日志';
        $this->display();
    }

    /**查看行为日志
     * @param int $id
     * @author leiying
     */
    public function edit($id = 0){
        if(I('post.btn')){$this->success('');}
        empty($id) && $this->error('参数错误！');

        $info = M('ActionLog')->field(true)->find($id);

        $this->assign('info', $info);
        $this->meta_title = '查看行为日志';
        $this->display();
    }

    /**删除行为日志
     * @param int $ids
     * @author leiying
     */
    public function remove($ids = 0){
        empty($ids) && $this->error('参数错误！');
        if(is_array($ids)){
            $map['id'] = array('in', $ids);
        }elseif (is_numeric($ids)){
            $map['id'] = $ids;
        }
        $res = M('ActionLog')->where($map)->delete();
        if($res !== false){
            $this->success('删除成功！');
        }else {
            $this->error('删除失败！');
        }
    }

    /**清空行为日志
     * @author leiying
     */
    public function clear(){
        $res = M('ActionLog')->where('1=1')->delete();
        if($res !== false){
            $this->success('日志清空成功！');
        }else {
            $this->error('日志清空失败！');
        }
    }

    /**列表
     * @author leiying
     */
    public function ActionList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_ActionList',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_ActionList',null);
            }
            $get = $search_data = session('searchdata_ActionList');
        }
        //搜索条件：行为唯一标识
        if(isset($get['name']) && $get['name'] != ''){
            $search_data['name'] = trim($get['name']);
            $where['name'] = array('like','%'.$search_data['name'].'%');
        }

        //搜索条件：行为说明
        if(isset($get['title']) && $get['title'] != ''){
            $search_data['title'] = trim($get['title']);
            $where['title'] = array('like','%'.$search_data['title'].'%');
        }
        $count = M('action')->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString =$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('action')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();
        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);
        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }


    /**批量操作
     * @param null $method
     * @author leiying
     */
    public function changeStatus($method=null){
        $id = I('post.id');
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'deletegroup':
                $res = M("action")->where(array("id"=>array('in',$id)))->delete();
                if($res){
                    $this->success("删除成功");
                }else{
                    $this->error("删除失败");
                }
                break;
            default:
                $this->error($method.'参数非法');
        }
    }

    /**添加
     * @author leiying
     */
    public function ActionAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            /*处理参数(请根据业务逻辑适当修改)*/
            //行为唯一标识
            $name = trim($post['name']);
            if(empty($name)){
                $this->error("行为唯一标识不能为空");
            }
            //行为说明
            $title = trim($post['title']);
            if(empty($title)){
                $this->error("行为说明不能为空");
            }
            //行为描述
            $remark = trim($post['remark']);
            if(empty($remark)){
                $this->error("行为描述不能为空");
            }
            //行为规则
            $rule = trim($post['rule']);
            if(empty($rule)){
                $this->error("行为规则不能为空");
            }
            //日志规则
            $log = trim($post['log']);
            if(empty($log)){
                $this->error("日志规则不能为空");
            }
            //类型
            $type = trim($post['type']);
            if(empty($type)){
                $this->error("类型不能为空");
            }
            //状态
            $status = trim($post['status']);
            if(empty($status)){
                $this->error("状态不能为空");
            }
            //修改时间
            $update_time = trim($post['update_time']);
            if(empty($update_time)){
                $this->error("修改时间不能为空");
            }
            $data = array(
                'add_time'=>time(),
                'name'=>$name,
                'title'=>$title,
                'remark'=>$remark,
                'rule'=>$rule,
                'log'=>$log,
                'type'=>$type,
                'status'=>$status,
                'update_time'=>$update_time,
            );
            $res = M('action')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }

    /**编辑
     * @author leiying
     */
    public function ActionEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            /*处理参数(请根据业务逻辑适当修改)*/
            //行为唯一标识
            $name = trim($post['name']);
            if(empty($name)){
                $this->error("行为唯一标识不能为空");
            }
            //行为说明
            $title = trim($post['title']);
            if(empty($title)){
                $this->error("行为说明不能为空");
            }
            //行为描述
            $remark = trim($post['remark']);
            if(empty($remark)){
                $this->error("行为描述不能为空");
            }
            //行为规则
            $rule = trim($post['rule']);
            if(empty($rule)){
                $this->error("行为规则不能为空");
            }
            //日志规则
            $log = trim($post['log']);
            if(empty($log)){
                $this->error("日志规则不能为空");
            }
            //类型
            $type = trim($post['type']);
            if(empty($type)){
                $this->error("类型不能为空");
            }
            //状态
            $status = trim($post['status']);
            if(empty($status)){
                $this->error("状态不能为空");
            }
            //修改时间
            $update_time = trim($post['update_time']);
            if(empty($update_time)){
                $this->error("修改时间不能为空");
            }
            $data = array(
                'add_time'=>time(),
                'name'=>$name,
                'title'=>$title,
                'remark'=>$remark,
                'rule'=>$rule,
                'log'=>$log,
                'type'=>$type,
                'status'=>$status,
                'update_time'=>$update_time,
            );
            $res = M('action')->where(array("id"=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('action')->where(array('id'=>$id))->find();
            $this->assign('res',$res);
            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

}